package com.corona.updater;

import java.awt.Component;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.swing.JOptionPane;

/* loaded from: pokewilds.jar:com/pkmngen/updater/VersionControl.class */
public class VersionControl {
    public String localVersion = "";
    public String latestVersion = "";
    private String versionEndpoint = "https://github.com/SheerSt/pokemon-wilds/releases/latest/download/version.txt";

    public void checkVersion() {
        try {
            URL url = new URL(this.versionEndpoint);
            InputStream in = url.openStream();
            url.openConnection().connect();
            File versionFile = new File("version.txt");
            if (!versionFile.exists()) {
                versionFile.createNewFile();
            }
            BufferedReader installed = new BufferedReader(new FileReader(versionFile));
            while (true) {
                String local = installed.readLine();
                if (local == null) {
                    break;
                }
                System.out.println("Currently installed: " + local);
                this.localVersion = local;
            }
            installed.close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            while (true) {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                System.out.println("Latest version: " + line);
                this.latestVersion = line;
            }
            reader.close();
            if (this.localVersion.equals("") || this.localVersion.isEmpty()) {
                this.localVersion = "Unknown";
            }
            if (this.localVersion.matches(this.latestVersion)) {
                System.out.println("Up to date");
            } else if (JOptionPane.showConfirmDialog((Component) null, "A new update is available!\nCurrently Installed: " + this.localVersion + "\nLatest: " + this.latestVersion + "\n\nClick to update.", "Update Available", 0) == 0) {
                runUpdater();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void runUpdater() throws IOException {
        Runtime.getRuntime().exec(" java -jar ./Updater.jar");
        System.exit(0);
    }

    public String GetLatestVersion() {
        return this.latestVersion;
    }
}
